﻿using IPCLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace IPCLibraryTest
{
    
    
    /// <summary>
    ///This is a test class for MemoryMappedFileStreamTest and is intended
    ///to contain all MemoryMappedFileStreamTest Unit Tests
    ///</summary>
	[TestClass()]
	public class MemoryMappedFileStreamTest
	{


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		[TestMethod()]
		public void FileStreamReadWriteTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile target = new MemoryMappedFile(size, name);

			BinaryReader br = new BinaryReader(target.GetStream());
			BinaryWriter bw = new BinaryWriter(target.GetStream());

			double expected = 1.23456;
			double actual;

			bw.Write(expected);
			actual = br.ReadDouble();
			Assert.AreEqual(expected, actual);

			int iexpect = 1234;
			bw.BaseStream.Position = 512;
			bw.Write(iexpect);
			br.BaseStream.Seek(512, SeekOrigin.Begin);
			int result = br.ReadInt32();

			Assert.AreEqual(iexpect, result);

			bw.Close();
			br.Close();
		}

		/// <summary>
		///A test for Read
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void ReadTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			byte[] data = new byte[] { 1, 2, 3, 4 };
			mmf.WriteBytes(data, 0, data.Length);

			byte[] buffer = new byte[size];
			int offset = 1;
			int count = 2;
			int expected = 2;
			int actual;
			actual = target.Read(buffer, offset, count);
			Assert.AreEqual(expected, actual);
			Assert.AreEqual(buffer[1], data[0]);
			Assert.AreEqual(buffer[2], data[1]);
			Assert.AreNotEqual(buffer[3], data[2]);

			target.Close();
		}
		/// <summary>
		///A test for Write
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void WriteTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			byte[] buffer = new byte[] { 5, 6, 7, 8 };
			int offset = 1; 
			int count = 2;
			target.Write(buffer, offset, count);

			byte[] result = mmf.ReadBytes(0, 4);
			Assert.AreNotEqual(buffer[0], result[0]);
			Assert.AreEqual(buffer[1], result[0]);
			Assert.AreEqual(buffer[2], result[1]);
			Assert.AreNotEqual(buffer[3], result[2]);
		}

		/// <summary>
		///A test for Position
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void PositionTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			long expected = 0;
			long actual;
			target.Position = expected;
			actual = target.Position;
			Assert.AreEqual(expected, actual);

			expected = size/2;
			target.Position = expected;
			actual = target.Position;
			Assert.AreEqual(expected, actual);

			target.Close();
		}

		/// <summary>
		///A test for Length
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void LengthTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			long actual;
			actual = target.Length;
			Assert.AreEqual(size, actual);

			target.Close();
		}

		/// <summary>
		///A test for CanWrite
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void CanWriteTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			Assert.IsTrue(target.CanWrite);

			target.Close();
		}

		/// <summary>
		///A test for CanSeek
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void CanSeekTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			Assert.IsTrue(target.CanSeek);

			target.Close();
		}

		/// <summary>
		///A test for CanRead
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void CanReadTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			Assert.IsTrue(target.CanRead);

			target.Close();
		}


		/// <summary>
		///A test for SetLength
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void SetLengthTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			long value = size / 2;
			try
			{
				target.SetLength(value);
				Assert.Fail("Should have thrown an exception.");
			}
			catch { }
			finally
			{
				target.Close();
			}
		}

		/// <summary>
		///A test for Seek
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void SeekTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			Assert.AreEqual(0, target.Position);
			target.Seek(10, SeekOrigin.Begin);
			Assert.AreEqual(10, target.Position);
			target.Seek(10, SeekOrigin.End);
			Assert.AreEqual(size - 10, target.Position);
			target.Seek(5, SeekOrigin.Current);
			Assert.AreEqual(size - 5, target.Position);

			target.Close();
		}

		/// <summary>
		///A test for Flush
		///</summary>
		[TestMethod()]
		[DeploymentItem("IPCLibrary.dll")]
		public void FlushTest()
		{
			int size = 1024;
			string name = "test";
			MemoryMappedFile mmf = new MemoryMappedFile(size, name);
			Stream target = mmf.GetStream();

			target.Flush();

			target.Close();
		}

	}
}
